package com.futurelight.recipes.adapter;

import android.graphics.Canvas;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.futurelight.recipes.R;
import com.futurelight.recipes.model.Result;

import java.util.List;

public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.MyViewHolder> {
    private final RecyclerView recyclerView;
    private final ItemTouchHelper itemTouchHelper;
    private List<Result> resultList;
    private CallBackResult callBack;

    public FavoriteAdapter(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        itemTouchHelper = new ItemTouchHelper(new ItemTouchCallBack());
        itemTouchHelper.attachToRecyclerView(recyclerView);

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater from = LayoutInflater.from(parent.getContext());
        View itemView = from.inflate(R.layout.item_favorite, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.setResult(resultList.get(position));
    }

    public void setResultList(List<Result> results) {
        resultList = results;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (resultList == null) {
            return 0;
        }
        return resultList.size();
    }

    public void setCallBack(CallBackResult callBack) {
        this.callBack = callBack;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        private final ImageView imageView;
        private final TextView title;
        private final TextView timer;
        private final TextView like;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.food_image);
            title = itemView.findViewById(R.id.title);
            timer = itemView.findViewById(R.id.timer);
            like = itemView.findViewById(R.id.likes);
            itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        itemTouchHelper.startDrag(MyViewHolder.this);
                        Log.v("ddd", "itemTouchHelper startDrag");
                    }
                    return false;
                }
            });
        }

        /**
         * ViewModel数据更新时，使用该方法更新数据
         * @param result
         */
        public void setResult(Result result) {

            Glide.with(itemView).load(result.getImage()).error(R.drawable.plate).into(imageView);
            timer.setText(String.valueOf(result.getReadyInMinutes()));
            title.setText(result.getTitle());
            like.setText(String.valueOf(result.getAggregateLikes()));

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.callBackResult(result);
                }
            });
        }
    }


    public class ItemTouchCallBack extends ItemTouchHelper.Callback {
        boolean deleteFlag = false;

        @Override
        public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            int swipeFlags = ItemTouchHelper.RIGHT ;
            int dragFlag = ItemTouchHelper.ACTION_STATE_IDLE;
            deleteFlag = false;
            return makeMovementFlags(dragFlag, swipeFlags);
        }

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        /**
         * 当滑动满足一定条件就调用删除方法删除该Item
         * @param viewHolder
         * @param direction
         */
        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            Log.v("ddd", "onSwiped");
            if (deleteFlag) {
                Log.v("ddd", "deleteFlag");
                remove(viewHolder.getAdapterPosition());
            }
        }

        @Override
        public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

            Log.v("ddd",String.valueOf(dX));
            if ((int) dX > viewHolder.itemView.getMeasuredWidth() * 0.4) {
                deleteFlag = true;
            } else {
                viewHolder.itemView.scrollTo((int) -dX, 0);
            }

        }
    }

    /**
     * 删除position对应的Item
     * @param position
     */
    public void remove(int position) {
        Log.v("ddd", String.valueOf(position));
        callBack.deleteCallBack(resultList.get(position));
        resultList.remove(position);
        notifyItemRemoved(position);
    }

}
